<?php
define('IMAGE_PATH', 'D:/Projects/gagcrawler/download/');
require('D:/Projects/gagcrawler/download/conf_image.php');
require('D:/Projects/gagcrawler/download/resize-class.php');
#define('IMAGE_PATH', '/home/wap247/public_html/9gag/uploads/');
$arrImg = array('png', 'jpg', 'jpeg', 'gif');
function generateThumbnail($arrOrigin, $arrSize){
	$path = null;
	$arrDim = null;
	foreach($arrSize as $size => $name){
		$arrDim = explode('x', $size);
		$path = IMAGE_PATH.$size.'/'.$arrOrigin['path'];
		if(!is_dir($path)){
			@mkdir($path, 0777, true);
		}
		$resizeObj = new resize(IMAGE_PATH.$arrOrigin['path'].$arrOrigin['name']);
		// *** 2) Resize image (options: exact, portrait, landscape, auto, crop)
		$resizeObj -> resizeImage($arrDim[0], $arrDim[1], $name);
		// *** 3) Save image
		$resizeObj -> saveImage($path.$arrOrigin['name'], 100);
	}
}
function imageDownloader($url){
	global $arrImg, $arrThumbSize;
	try{		
		$ch = curl_init();
		$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
		$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
		$header[] = "Cache-Control: max-age=0";
		$header[] = "Connection: keep-alive";
		$header[] = "Keep-Alive: 300";
		$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
		$header[] = "Accept-Language: en-us,en;q=0.5";
		$header[] = "Pragma: "; // browsers keep this blank.
		// set URL and other appropriate options
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
		#curl_setopt($ch, CURLOPT_PROXY, '210.245.0.25:80');
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);

		$responseContent = curl_exec($ch);
		if(strlen($responseContent) <= 1300) return false;
		$todayFolder = date('Y/m/d/i/');
		$ext = strtolower(end(explode('.', $url)));
		$ext = preg_replace('/(.+)\?.*/is', '$1', $ext);// remove param in URL
		if(!in_array($ext, $arrImg)) return false;
		$fileName = md5($url).'.'.$ext;
		if(!file_exists(IMAGE_PATH.$todayFolder)) mkdir(IMAGE_PATH.$todayFolder, 0777, true);

		if(file_put_contents(IMAGE_PATH.$todayFolder.$fileName, $responseContent)){
			generateThumbnail(array('path' => $todayFolder, 'name' => $fileName), $arrThumbSize);
		}
		return array('name' => $todayFolder.$fileName, 'checksum' => md5($responseContent));
	}
	catch(Exception $e){
		return false;
	}
}
function isAnimatedImage($filename) {
    if(!($fh = @fopen($filename, 'rb')))
        return false;
    $count = 0;
    //an animated gif contains multiple "frames", with each frame having a
    //header made up of:
    // * a static 4-byte sequence (\x00\x21\xF9\x04)
    // * 4 variable bytes
    // * a static 2-byte sequence (\x00\x2C) (some variants may use \x00\x21 ?)
   
    // We read through the file til we reach the end of the file, or we've found
    // at least 2 frame headers
    while(!feof($fh) && $count < 2) {
        $chunk = fread($fh, 1024 * 100); //read 100kb at a time
        $count += preg_match_all('#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $chunk, $matches);
   }
   
    fclose($fh);
    return $count > 1;
}